//项目的亮点 = 像nuxt一样可以自动进行配置

//项目的难点 = 扁平路由转换成树形

//require.context('路径','是否递归引用','正则') 一次导入多个模块

///\.vue$/代表匹配所有.vue的文件
let files = require.context("@/pages", true, /\.vue$/);

// console.log(files.keys()); //files.key()方法是遍历,是可以拿到所有路径

//配置路由
//遍历
let repeatRoutes = files.keys().map((path) => {
  //通过正则,去掉index.vue或者去掉.vue的后缀
  let regExp = /(\/index\.vue|\.vue)$/;
  //substring是把第一个字符'/'给去掉,replace是把满足正则条件的index.vue或者.vue替换成''
  //获取路径对应的每个.vue组件
  let currentComponent = files(path).default;
  let { meta, name, redirect } = currentComponent;

  path = path.substring(1).replace(regExp, "");
  // console.log(path);

  return {
    path: path ? path : "/pages",
    //重定向,name属性,meta路由元信息
    meta: meta ?? {},
    name: name ?? path.substring(1).split("/").join("-").toLowerCase(),
    redirect: redirect ?? null,
    component: () => import("@/pages" + path),
  };
});

// console.log(repeatRoutes);

function convertTree(repeatRoutes) {
  repeatRoutes = [...repeatRoutes];
  let treeRoutes = [];
  //先给数据添加id和pid
  repeatRoutes.forEach((route) => {
    //将路径去掉第一个 / 转换成一个数组
    let routePathArr = route.path.substring(1).split("/");
    //length为1 的都是根节点
    //需要添加id和pid

    // 正常添加id
    route.id = routePathArr.join("-");
    if (routePathArr.length > 1) {
      //浅拷贝routePathArr
      let pathArr = [...routePathArr];
      pathArr.pop();
      let pid = pathArr.join("-");
      route.pid = pid; //添加父节点id
    }
  });

  //转换成树形数据

  repeatRoutes.forEach((route) => {
    //将所有的路径都变成小写,目的是为了和菜单的高亮效果绑定
    route.path = route.path.toLowerCase();
    if (route.path === "/page404") {
      route.path = "*";
    }
    //找爸爸
    let parent = repeatRoutes.find((v) => route.pid === v.id);
    if (parent) {
      if (!parent.children) {
        parent.children = [];
      }
      route.path = route.path.substring(1).split("/").pop();
      parent.children.push(route);
    } else {
      treeRoutes.push(route);
    }
  });

  return treeRoutes;
}
let res = convertTree(repeatRoutes);
console.log(res);

export default [...res];
